﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;
using System.Windows.Forms;
using XCI.Extensions;

namespace XCI.Sys.Forms
{
    public partial class FrmLogDetails : FrmBase
    {
        private GridHelper<SysLog> helper;
        public FrmLogDetails(GridHelper<SysLog> p_helper)
        {
            InitializeComponent();
            helper = p_helper;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, () => { EditFormEscResultCancel(); });
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            BindModel();
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            if (helper!=null)
            {
                btnPrev.Enabled = !helper.IsFirstFocus;
                btnNext.Enabled = !helper.IsLastFocus;
            }
            var model = LogService.Instance.SelectById(helper.SelectedId);
            if (model == null) return;
            if (model.Message.IsNotEmpty()) model.Message = model.Message.Replace("\n", "\r\n");
            if (model.Details.IsNotEmpty()) model.Details = model.Details.Replace("\n", "\r\n");
            if (model.ParamData.IsNotEmpty()) model.ParamData = model.ParamData.Replace("\n", "\r\n");
            if (model.Result.HasValue && model.Result.Value)
            {
                editResultText.ForeColor = Color.Green;
            }
            else
            {
                editResultText.ForeColor = Color.Red;
            }


            WinHelper.SetFormData(layoutPanel, model);
            
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmLogDetails_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            helper.View.MovePrev();
            BindModel();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            helper.View.MoveNext();
            BindModel();
        }
    }
}